<?php

/* * 
 * 照片
 */
namespace Api\Model;
use Think\Model;
class PhotoModel extends Model {

    //获取照片列表
    public function getPhotoListByAlbumId($albumId, $pageNum) {

        $map['p.album_id'] = $albumId;

        $list = $this->alias('p')
            ->join("LEFT JOIN __ACTIVITY__ a ON p.reference_id = a.activity_id AND p.photo_type = ". C("PHOTO_TYPE.ACTIVITY"))
            ->join("LEFT JOIN __CHALLENGE__ c ON p.reference_id = c.challenge_id AND p.photo_type = ". C("PHOTO_TYPE.CHALLENGE"))
            ->field("p.photo_id, DATE_FORMAT(p.create_time,'%Y-%m-%d') as create_time, p.thumb_url, p.photo_url, p.photo_type,
            p.collect_count, p.comment_count, a.activity_id, a.activity_name, c.challenge_name, c.category_name as challenge_category_name")
            ->where($map)
            ->order("create_time ASC")
            ->limit(($pageNum-1)*C('DEFAULT_PAGE_SIZE') . ',' . C('DEFAULT_PAGE_SIZE'))
            ->select();

        if(!isNullOrEmpty($list)) {
            foreach ($list as &$item) {
                $item['activity_category_list'] = D('categoryRelationship')->getCategoryList($item['activity_id'], C("CATEGORY_RELATIONSHIP_TYPE.ACTIVITY"));
            }
        }

        return $list;
    }

    //获取相册中的第一张照片
    public function getFirstPhotoByAlbumId($albumId) {

        $map['album_id'] = $albumId;

        $photo = $this
            ->field("photo_id, thumb_url")
            ->where($map)
            ->order("create_time ASC")
            ->find();

        return $photo;

    }

    //获取照片（基本信息）
    public function getPhotoById($id) {

        $photo = $this->alias('p')
            ->join("LEFT JOIN __ACTIVITY__ a ON p.reference_id = a.activity_id AND p.photo_type = ". C("PHOTO_TYPE.ACTIVITY"))
            ->join("LEFT JOIN __CHALLENGE__ c ON p.reference_id = c.challenge_id AND p.photo_type = ". C("PHOTO_TYPE.CHALLENGE"))
            ->field("p.photo_id, DATE_FORMAT(p.create_time,'%Y-%m-%d') as create_time, p.thumb_url, p.photo_url, p.photo_type,
            p.collect_count, p.comment_count, a.activity_id, a.activity_name, c.challenge_name, c.category_name as challenge_category_name")
            ->where(array('photo_id' => $id))
            ->find();

        $photo['activity_category_list'] = D('categoryRelationship')->getCategoryList($photo['activity_id'], C("CATEGORY_RELATIONSHIP_TYPE.ACTIVITY"));

        return $photo;
    }

    //获取照片详情（包含用户信息、照片描述）
    public function getPhotoDetailById($id) {

        $map['p.photo_id'] = $id;

        $photo = $this->alias('p')
            ->join("LEFT JOIN __USER__ u ON p.user_id = u.user_id")
            ->field("p.photo_id, p.photo_url, DATE_FORMAT(p.create_time,'%m-%d %H:%i') as create_time, 
            u.user_id, u.username, u.head_image_url, u.city")
            ->find();

        return $photo;
    }

    //删除单张照片
    public function deletePhotoByIds($ids) {

        $map['photo_id'] = array('in', $ids);

        $photo = $this->where($map)->find();

        //删除照片
        $this->where($map)->delete();

        //更新相册中的照片数量
        $albumData['photo_count'] = $this->getPhotoCountByAlbumId($photo['album_id']);
        D('album')->where(array('album_id' => $photo['album_id']))->save($albumData);

        return "SUCCESS";

    }

    //删除相册中的所有照片
    public function deletePhotoByAlbumId($albumId) {

        $map['album_id'] = $albumId;

        $this->where($map)->delete();

        //统计相册中的照片数量
        $albumData['photo_count'] = $this->getPhotoCountByAlbumId($albumId);
        D('album')->where(array('album_id' => $albumId))->save($albumData);

    }

    //根据相册ID获取照片数量
    public function getPhotoCountByAlbumId($albumId) {

        $map['album_id'] = $albumId;

        $count = $this->where($map)->count();

        return $count;

    }

    //批量添加照片（Json）
    public function batchAddPhoto($photoJson, $albumId, $referenceId, $type, $userId, $description='') {

        if(isNullOrEmpty($photoJson)) {
            return "ERROR_NECESSARY_DATA_NOT_FOUND";
        }

        $albumRecord = D('album')->getAlbumById($albumId);

        if(isNullOrEmpty($albumRecord)) {
            return "ERROR_ALBUM_NOT_FOUND";
        }

        $photoList = unserialize($photoJson);

        foreach ($photoList as $photo) {

            $photoData[] = array('create_time' => getCurrentDate(), 'user_id' => $userId, 'album_id' => $albumId,
                                 'thumb_url' => $photo['thumb'], 'photo_url' => $photo['original'], 'photo_type' => $type,
                                 'reference_id' => $referenceId, 'collect_count' => 0, 'comment_count' => 0, 'description' => $description);
        }

        $this->addAll($photoData);

        //统计相册中的照片数量
        $albumData['photo_count'] = $this->getPhotoCountByAlbumId($albumId);
        D('album')->where(array('album_id' => $albumId))->save($albumData);

        return "SUCCESS";

    }

}

